package com.dianshang.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dianshang.dao.AdminDao;
import com.dianshang.pojo.system.Admin;
import com.dianshang.service.system.AdminService;
import com.dianshang.utils.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminDao adminDao;

    /**
     * 查询管理员
     */
    public Admin findByName(String username) {
        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName",username); // 用户名 密码
        criteria.andEqualTo("status","1"); // 状态
        return adminDao.selectByExample(example).get(0);
    }

    /**
     * 添加用户与角色
     */
    public void insert(Map<String, Object> formData) {
        Admin admin = new Admin();
        admin.setLoginName((String) formData.get("loginName"));
        admin.setPassword(
                BCrypt.hashpw((String) formData.get("password"), BCrypt.gensalt())
        );
        adminDao.insertAdmin(admin);
        adminDao.insertAdminRole(admin.getId(),(Integer) formData.get("roleId"));
    }

}